package com.yifi.system.controller;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.SysUtil;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.system.service.MisOrgService;

import cn.hutool.core.util.StrUtil;

/** 
* @ClassName: SysOrgController 
* @Description: TODO
* @author 姚善利
* @date 2021-10-19 14:21:21 
*  
*/
@Path(value="/sysorg",viewPath = "/")
public class SysOrgController extends BaseController{
	@Inject
	private MisOrgService misOrgService;
	
	public void index()
	{
		String ccode = ToolSession.getUser(this).getCcode();
		String c_pro = ToolSession.getUser(this).getC_pro();
		set("c_pro", c_pro);
		render("system/sysorg.html");
	}
	
	public void list()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		String cname=getPara("cname","");
		//List<Record> list=misOrgService.getTreeIdPid("id", "cname", null);
		String sql="select * from mis_org where 1=1 ";
		if(StrUtil.isNotEmpty(cname))
		{
			sql+=" and cname like '%"+cname+"%'";
		}
		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and c_pro='"+c_pro+"' ";
		}
		
		sql+=" order by isort,ccode,pcode";
		List<Record> list=Db.find(sql);
		renderJson(list);
	}
	
	public void getData()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		String ccode=getPara("ccode","");
		String sql="select * from mis_org where ccode='"+ccode+"'";
		if(StrUtil.isNotEmpty(c_pro))
		{
			sql+=" and c_pro='"+c_pro+"' ";
		}
		//sql+=" and c_pro='"+c_pro+"' ";
		Record record=Db.findFirst(sql);
		renderJson(record);
	}
	
	public void save()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		
		String pcode=getPara("pcode","");
		
		String ccode=getPara("ccode","");
		String cname=getPara("cname","");
		String ilayer=getPara("ilayer","");
		String cgroup=getPara("cgroup","");
		String ctype = cgroup.equals("2")?"1":"0";
		String m_leader=getPara("m_leader","");
		String isort=getPara("isort","1");
		
		String sql="select count(1) from mis_org where cname='" + cname + "'"
				+ " and ccode<>'" + ccode + "' and c_pro='"+c_pro+"' ";
		if(Db.queryInt(sql)>0)
		{
			renderJson(error("部门名称重复，请重新填写！"));
			return;
		}
		
		sql="select * from mis_org where ccode='"+ccode+"'";
		Record record=Db.findFirst(sql);
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record=new Record();
			
	        //自动编号
	        ccode = "01";
	        sql = "select max(ccode) from mis_org where ilayer=" + ilayer;
	        if (!pcode.equals(""))
	        {
	            sql += " and ccode like '" + pcode + "%' and ccode<>'" + pcode + "'";
	        }
	        String maxcode = Db.queryStr(sql);
	        if (StrUtil.isEmpty(maxcode))
	        {
	            ccode = pcode + ccode;
	        }
	        else
	        {
	        	ccode = String.valueOf(Integer.parseInt(maxcode)+1);
	        	ccode = SysUtil.getFillNum(ccode,Integer.parseInt(ilayer)*2);
	        }
			record.set("ccode",ccode);
			record.set("pcode",pcode);
			record.set("ilayer",ilayer);
		}
		record.set("cname",cname);
		record.set("ctype",ctype);
		record.set("cgroup",cgroup);
		record.set("m_leader",m_leader);
		record.set("isort",isort);
		record.set("c_pro",c_pro);
		
		if(DbUtils.save("mis_org", "ccode", record, isedit))
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
	}
	
	public void edit()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		
		String ccode=getPara("ccode","");
		String pcode=getPara("pcode","");
		int ilayer=getParaToInt("ilayer",1);
		ilayer=ilayer+1;
		
		set("ccode", ccode);
		set("pcode", pcode);
		set("ilayer", ilayer);
		set("c_pro", c_pro);
		
		render("system/sysorg_edit.html");
	}
	
	public void remove()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String ccode=getPara("ccode","");
		String sql="select count(1) from mis_org where "
				+ "((ccode like '"+ccode+"%' and ccode<>'"+ccode+"') or pcode='"+ccode+"') ";

		if(Db.queryInt(sql)>0)
		{
			renderJson(error("存在子部门，删除失败！"));
			return;
		}
		
		sql="select count(1) from mis_user where cdept='"+ccode+"' ";
		if(Db.queryInt(sql)>0)
		{
			renderJson(error("存在用户，删除失败！"));
			return;
		}
		
		sql="delete from mis_org where ccode='"+ccode+"'";
		if(Db.update(sql)>0)
		{
			renderJson(success("删除成功！"));
		}
		else
		{
			renderJson(error("删除失败！"));
		}
	}
	//所有领导
	public void getLeadList()
	{
		String c_pro=getPara("c_pro","");
		if(StrUtil.isEmpty(c_pro))
		{
			c_pro = ToolSession.getUser(this).getC_pro();
		}
		String sql = "select ccode,cname from mis_user where is_ld='1' ";
        sql+=" and c_pro='"+c_pro+"' ";
		sql+=" order by cname";
		renderJson(Db.find(sql));
	}
	
	//弹框选择部门
	public void select()
	{
		String c_pro=getPara("c_pro","");
		set("c_pro", c_pro);
		render("system/sysorg_select.html");
	}
}
